
use dta/LCD_v13.dta, clear

replace days_nat=0 if days_nat==.
keep if running_loc==1

******************************************************************************************************
********************** Drop small parties, and direct mayor*******************************************
******************************************************************************************************
keep if year==2003 | year==2007 | year ==2011
keep if party_loc=="a" |party_loc=="frp" |party_loc=="h" |party_loc=="krf" |party_loc=="sp"  |party_loc=="sv" |party_loc=="v"  
sum year
keep if directmayor_loc==0
sum year

******************************************************************************************************
********************** Drop cand. running for parliament before **************************************
******************************************************************************************************

*** run and win before; run and win after; run and win ever
foreach var in run win {
gen cand`var'_nat_before=0
replace cand`var'_nat_before=1 if cand`var'_nat_firstyear < year & cand`var'_nat_firstyear !=.
gen cand`var'_nat_after=0
replace cand`var'_nat_after=1 if cand`var'_nat_lastyear > year & cand`var'_nat_lastyear !=.
gen cand`var'_nat_ever=0
replace cand`var'_nat_ever=1 if cand`var'_nat!=0 
}

drop if candrun_nat_before==1
sum year

******************************************************************************************************
********************** Drop leapfrogging mayors ******************************************************
******************************************************************************************************
gen temp=0
replace temp=1 if mayor_loc==1 & rank_loc!=1
li knr_loc year party_loc rank_loc candidatename_orig_loc if temp==1
egen sumtemp=sum(temp), by(year knr_loc party_loc)
drop if sumtemp==1

******************************************************************************************************
********************** Put outside top 10 at top 10 **************************************************
******************************************************************************************************
replace rank_loc=10 if rank_loc>10

******************************************************************************************************
********************** Create variables **************************************************************
******************************************************************************************************
tab partyname_loc, gen(d_party)
tab year, gen(d_year)
egen knr_locyear=group(knr_loc year)
egen partyyear=group(party_loc year)
egen partydistrictyear=group(party_loc cnr_loc year)

egen list_mayor_loc=sum(mayor_loc), by(year knr_loc party_loc)
egen list_id=group(year knr_loc party_loc)

tab rank_loc, gen(d_rank_loc)
forvalues i=1(1)10 {
gen d_rank_loc`i'xfemale=d_rank_loc`i'*female
}

gen  party_votes_locvoteshare=party_votes_loc/totalvotes
label var  party_votes_locvoteshare "List votes (share of total)"

egen total_personalvotes_total_loc=sum(personalvotes_total_loc), by(knr_loc party_loc year)
gen personalvoteshare=personalvotes_total_loc/total_personalvotes_total_loc
label var personalvoteshare "Personal votes (share of party total)"
replace personalvoteshare=. if personalvotes_total_loc==.
replace personalvoteshare=. if total_personalvotes_total_loc==.

label var female "Gender (Female=1)"
label var age "Age in election year"

sum days_nat candrun_nat_after candwin_nat_after

unique knr_loc
sum knr_loc

******************************************************************************************************************************
************************************ PLOTTING RAW DATA   *********************************************************************
******************************************************************************************************************************
*** keep only lists WITHOUT mayor
keep if list_mayor_loc==0


preserve
	collapse days_nat candrun_nat_after candwin_nat_after, by(female rank_loc)
	
	*** to get numbers reported in paper
	list
	
		twoway (scatter candrun_nat_after rank_loc if female==1, msymbol(O) mcolor(black) connect(l) lpattern(solid) lcolor(black))(scatter candrun_nat_after  rank_loc if female==0, msymbol(S) mcolor(gray) connect(l) lpattern(solid) lcolor(black)), legend(off) ytitle(Fraction) title(Run) xtitle("Rank") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10+") xscale(reverse range(0.9 10.1)) legend(order(1 "Women" 2 "Men")) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono) ylabel(0(0.1)0.5, grid angle(0)) yline(0.5, lstyle(foreground) lcolor(gs14))
		graph save figures/gph/base_candrun_nat_after.gph, replace
		
		twoway (scatter candwin_nat_after rank_loc if female==1, msymbol(O) mcolor(black) connect(l) lpattern(solid) lcolor(black))(scatter candwin_nat_after  rank_loc if female==0, msymbol(S) mcolor(gray) connect(l) lpattern(solid) lcolor(black)), legend(off) ytitle(Fraction) title(Win) xtitle("Rank") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10+") xscale(reverse range(0.9 10.1)) legend(order(1 "Women" 2 "Men")) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono) ylabel(0(0.02)0.1, grid angle(0) gmax) 
		graph save figures/gph/base_candwin_nat_after.gph, replace
		
restore

******************************************************************************************************************************
************************************ Party-district-year FE ******************************************************************
******************************************************************************************************************************

foreach var in candrun_nat_after {
		xtreg `var' d_rank_loc1x-d_rank_loc10x d_rank_loc1-d_rank_loc10, i(partydistrictyear) fe cluster(partydistrictyear)
		estimates store w
		
coefplot w, drop(*party* *year* *cons d_rank_loc1 d_rank_loc2 d_rank_loc3 d_rank_loc4 d_rank_loc5 d_rank_loc6 d_rank_loc7 d_rank_loc8 d_rank_loc9 d_rank_loc10) yline(0) vertical legend(off) scheme(s2mono) ytitle("Estimate") xtitle("Rank") graphregion(color(white))  ylabel(, angle(0)) ylabel(, angle(0)) yscale(range(-0.1 0.25)) ylabel(-0.1(0.1)0.2) ///
xlab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10+") xsc(reverse) 
graph save figures/gph/_partydistrict_`var'.gph, replace
estimates clear
}

foreach var in candwin_nat_after {
		xtreg `var' d_rank_loc1x-d_rank_loc10x d_rank_loc1-d_rank_loc10, i(partydistrictyear) fe cluster(partydistrictyear)
		estimates store w
		
coefplot w, drop(*party* *year* *cons d_rank_loc1 d_rank_loc2 d_rank_loc3 d_rank_loc4 d_rank_loc5 d_rank_loc6 d_rank_loc7 d_rank_loc8 d_rank_loc9 d_rank_loc10) yline(0) vertical legend(off) scheme(s2mono) ytitle("Estimate") xtitle("Rank") graphregion(color(white))  ylabel(, angle(0)) yscale(range(-0.05 0.125)) ylabel(-0.05(0.05)0.10)  ///
xlab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10+") xsc(reverse) 
graph save figures/gph/_partydistrict_`var'.gph, replace
estimates clear
}

******************************************************************************************************************************
******************************************************************************************************************************
******************************************************************************************************************************

cd figures/gph/
grc1leg base_candrun_nat_after.gph base_candwin_nat_after.gph ///
_partydistrict_candrun_nat_after.gph _partydistrict_candwin_nat_after.gph ///
, graphregion(fcolor(white) lcolor(white)) scheme(s2mono) cols(2) 
graph export ../FigureA5.pdf, replace
cd ../../
